package com.youmingxing.adpter.selfInfo;


import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youmingxing.R;
import com.youmingxing.activity.selfInfo.PersonalInfoActivity;
import com.youmingxing.bean.selfinfo.MessageList;
import com.youmingxing.utils.ViewHolderUtils;

import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * 私信列表 Adaoter
 */
public class MessageListAdapter extends BaseAdapter {
    private List<MessageList> messageLists;
    private TextView nameTv;
    private TextView timeTv;
    private TextView messageNumTv;

    private TextView contentTv;
    private ImageView icon;

    private Context mContext;


    public MessageListAdapter(Context mContext, List<MessageList> messageLists) {
        this.mContext = mContext;
        this.messageLists = messageLists;
    }

    @Override
    public int getCount() {
        return messageLists == null ? 0 : messageLists.size();
    }

    @Override
    public Object getItem(int position) {
        if ((null != messageLists && messageLists.size() > 0) && (position >= 0 && position < messageLists.size())) {
            return messageLists.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.messagelist_item, null);
        }
        nameTv = ViewHolderUtils.getViewHolderView(convertView, R.id.user_name);
        messageNumTv = ViewHolderUtils.getViewHolderView(convertView, R.id.message_num);
        timeTv = ViewHolderUtils.getViewHolderView(convertView, R.id.user_time);
        contentTv = ViewHolderUtils.getViewHolderView(convertView, R.id.message_contents);


        icon = ViewHolderUtils.getViewHolderView(convertView, R.id.user_icon);
        nameTv.setText(messageLists.get(position).nickname);
        messageNumTv.setText("" + "");
        contentTv.setText(messageLists.get(position).nickname);
        timeTv.setText(messageLists.get(position).votes + "票");

        Glide.with(mContext).load(messageLists.get(position).headimg).bitmapTransform(new
                CropCircleTransformation(mContext))
                .placeholder(R
                        .drawable.default_avatar).crossFade().into(icon);
        icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PersonalInfoActivity.class);
                intent.putExtra("u_id", messageLists.get(position).u_id);
                mContext.startActivity(intent);
            }
        });

        return convertView;
    }
}